package com.yunban.project.template.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.StyleMapper;
import com.yunban.project.template.model.domain.Style;
import com.yunban.project.template.model.dto.StyleReq;
import com.yunban.project.template.model.vo.StyleResp;
import com.yunban.project.template.service.StyleService;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lijiajun
 * @date: 2024-11-06
 * @version: 1.0
 */
@Slf4j
@Service
public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements StyleService {
    @Override
    public Style findById(long id) {
        Style style = this.getById(id);
        if (ObjectUtils.isEmpty(style)) {
            throw new BusinessException("风格信息不存在");
        }
        return style;
    }

    @Override
    public List<Tree<Integer>> tree() {
        // 获取所有风格
        List<StyleResp> styleList = SmartPageUtil.convert(this.list(), StyleResp.class);
        // 假设pid==null 那么赋初始值0
        styleList = styleList.stream().peek(style -> {
            if (style.getPid() == null) {
                style.setPid(0L);
            }
        }).collect(Collectors.toList());
        log.info("styleList:{}", styleList);
        // 构建树结构
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setParentIdKey("pid");
        // 构建tree
        return TreeUtil.build(styleList, 0, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId().intValue());
            tree.setParentId(treeNode.getPid().intValue());
            tree.setName(treeNode.getName());
        });

    }

    @Override
    public Boolean saveStyle(StyleReq req) {
        if (req.getPid() != null) {
            if (ObjectUtils.isEmpty(this.getById(req.getPid()))) {
                throw new BusinessException("父级风格不存在");
            }
        }
        Style style = new Style();
        BeanUtils.copyProperties(req, style);
        return this.save(style);
    }

    @Override
    public Boolean editStyle(long id, StyleReq req) {
        Style style = this.findById(id);
        if (req.getPid() != null) {
            if (ObjectUtils.isEmpty(this.getById(req.getPid()))) {
                throw new BusinessException("父级风格不存在");
            }
        }
        BeanUtils.copyProperties(req, style);
        return this.updateById(style);
    }

    @Override
    public Boolean delStyle(long id) {
        Style style = this.findById(id);
        return this.removeById(style);
    }
}
